#!/bin/bash

install_mysql(){
	echo "mysql5.7 rpm安装"
    #源码安装 这个是很稳定的版本
    wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
    yum install -y mysql57-community-release-el7-11.noarch.rpm
    yum clean all          
    yum makecache          
    yum install --nogpgcheck -y mysql-community-server
    echo "启动MySQL服务..."
    systemctl start mysqld
    systemctl enable mysqld
    sleep 5
    temp_password=$(grep 'temporary password' /var/log/mysqld.log | awk '{print $NF}')
    echo "运行MySQL安全配置..."
    mysql --connect-expired-password -uroot -p"$temp_password" << EOF
    SET GLOBAL validate_password_policy=LOW;
    SET GLOBAL validate_password_length=4;
    ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
    FLUSH PRIVILEGES;
EOF

    echo "MySQL 5.7安装完成！"
    echo "Root密码: 123456"
}
install_nginx(){
	yum install -y gcc make pcre-devel zlib-devel wget tar
    wget http://nginx.org/download/nginx-1.26.3.tar.gz
    tar -zxvf nginx-1.26.3.tar.gz
    cd nginx-1.26.3
    ./configure --prefix=/usr/local/nginx 
    make && make install
    firewall-cmd --permanent --add-service=http
    firewall-cmd --permanent --add-service=https
    firewall-cmd --reload
    # 临时export增加环境变量
    export PATH=/usr/local/nginx/sbin:$PATH
    # 永久增加环境变量
    echo 'export PATH=/usr/local/nginx/sbin:$PATH' >> /etc/profile
    source /etc/profile
    nginx -v
    # 启动
    nginx 
}
install_php(){
	yum -y install php-fpm php-cli php-mysqlnd php-json php-gd php-mbstring php-xml
    sed -i 's/user = apache/user = nginx/g; s/group = apache/group = nginx/g; s/;listen.owner = nobody/listen.owner = nginx/g; s/;listen.group = nobody/listen.group = nginx/g;s/listen.acl_users = apache,nginx/;listen.acl_users = apache,nginx/g;s/;listen.mode = 0660/listen.mode = 0660/g' /etc/php-fpm.d/www.conf

    sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
	setenforce 0
	
    sed -i '1i user nginx;' /usr/local/nginx/conf/nginx.conf
    sed -i '/^[[:space:]]*server {/,/^[[:space:]]*}/ {
        /^[[:space:]]*server_name[[:space:]]*localhost;/a\
        location ~ \\.php$ {\
            fastcgi_pass unix:/run/php-fpm/www.sock;\
            fastcgi_index index.php;\
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;\
            include fastcgi_params;\
        }
    }' /usr/local/nginx/conf/nginx.conf

    systemctl restart php-fpm
    nginx -s reload

    echo "<?php phpinfo(); ?>" | sudo tee /usr/local/nginx/html/info.php
}
remove_mysql(){
	#这个是mysql的卸载完整版

    echo "开始清理MySQL..."

    # 停止MySQL服务
    echo "停止MySQL服务..."
    systemctl stop mysqld 2>/dev/null || true
    systemctl disable mysqld 2>/dev/null || true

    # 卸载MySQL相关包
    echo "卸载MySQL软件包..."
    yum remove -y mysql-community-server mysql-community-client mysql-community-common mysql-community-libs mysql-community-devel 2>/dev/null || true
    yum remove -y mysql-server mysql-client mysql-common mysql-libs 2>/dev/null || true

    # 删除MySQL数据目录
    echo "删除MySQL数据文件..."
    rm -rf /var/lib/mysql
    rm -rf /var/log/mysqld.log

    # 删除MySQL配置文件
    echo "删除MySQL配置文件..."
    rm -f /etc/my.cnf
    rm -f /etc/my.cnf.d/*
    rm -f /etc/yum.repos.d/mysql57-community.repo
    rm -f /etc/yum.repos.d/mysql57.repo

    # 删除MySQL用户和组
    echo "清理MySQL用户..."
    userdel mysql 2>/dev/null || true
    groupdel mysql 2>/dev/null || true

    # 清理yum缓存
    echo "清理yum缓存..."
    yum clean all

    # 删除临时文件
    echo "删除临时文件..."
    rm -f mysql57-community-release-el7-11.noarch.rpm 2>/dev/null || true

    echo "MySQL清理完成！"
    echo "系统已恢复到安装前的状态"
}
remove_nginx(){
	#!/bin/bash

    echo "开始卸载Nginx..."

    # 停止服务
    systemctl stop nginx 2>/dev/null || true
    systemctl disable nginx 2>/dev/null || true

    # 卸载Nginx
    yum remove -y nginx

    # 删除配置文件和数据
    rm -rf /etc/nginx
    rm -rf /var/log/nginx
    rm -rf /usr/share/nginx
    rm -rf /usr/local/nginx
    rm -f /etc/yum.repos.d/nginx.repo

    # 清理缓存
    yum clean all

    echo "Nginx卸载完成！"

}
remove_php(){
    yum -y remove php-fpm php-cli php-mysqlnd php-json php-gd php-mbstring php-xml
    yum autoremove
    rm -rf /etc/php-fpm.d/ /var/lib/php/ /run/php-fpm/
}

show_menu() {
    echo -e "\n===== 环境安装脚本 ====="
    echo "1. 安装 mysql"
    echo "2. 安装 nginx"
    echo "3. 安装 php"
    echo "4. 卸载 mysql"
    echo "5. 卸载 nginx"
    echo "6. 卸载 php"
    echo "7. 退出程序"
    read -p "请选择（1-7）：" choice
}

while true;do
	show_menu
	case "$choice" in 
		1) install_mysql;;
		2) install_nginx;;
		3) install_php;;
		4) remove_mysql;;
		5) remove_nginx;;
		6) remove_php;;
		7) echo "退出";exit 0;;
		*) echo "输入无效，请重新选择！" ;;
	esac
done
